package com.ac.input.web.form;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.caac.core.util.StringUtil;
import com.caac.core.web.form.BaseForm;
import com.ac.input.dm.HndInpDef;
import com.ac.input.dm.HndInpDefItm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class DataForm extends BaseForm {
	private static final long serialVersionUID = 1L;
	private String mendId;
    private String oldMendId;
    private String whereStr;
    private String keyValue;
    private String flag;
    private String isDef;
    private String acPath;
    private Date curDate;
    private String ipAddr;

    private HndInpDef rhid;

    private HashMap hMap = new HashMap();

    public HndInpDef getRhid() {
        return rhid;
    }

    public void setRhid(HndInpDef rhid) {
        this.rhid = rhid;
    }

    public HashMap getHMap() {
        return hMap;
    }

    public void setHMap(HashMap hMap) {
        this.hMap = hMap;
    }

    public void setDynaProperty(String key, Object value) {
        this.hMap.put(key, value);
    }

    public Object getDynaProperty(String key) {
        return this.hMap.get(key);
    }

    public String getMendId() {
        return mendId;
    }

    public void setMendId(String mendId) {
        this.mendId = mendId;
    }

    public String getOldMendId() {
        return oldMendId;
    }

    public void setOldMendId(String oldMendId) {
        this.oldMendId = oldMendId;
    }

    public List getViews() {
        return getRhid().getViewClass().getChildren();
    }

    public List getKeys() {
        return getRhid().getViewClass().getDmKeyList();
    }

    public String getWhereStr() {
        return whereStr;
    }

    public void setWhereStr(String whereStr) {
        this.whereStr = whereStr;
    }

    public String getUpdateSql() {
        String sql = "Update " + getRhid().getTabName() + " Set ";
        List vList = getRhid().getCols();
        for (int i = 0; i < vList.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) vList.get(i);
            String strTemp = StringUtil.nullToStr(getDynaProperty(dm
                    .getColCode()));
            if ("1".equals(dm.getVisTyp())) {
                sql += dm.getColCode() + "='" + StringUtil.toSQL(strTemp)
                        + "',";
            } else if ("3".equals(dm.getVisTyp())) {
                sql += dm.getColCode() + "= to_date('" + strTemp
                        + "','yyyy-mm-dd'),";
            } else {
                sql += dm.getColCode() + "='" + strTemp + "',";
            }
        }
        return sql.substring(0, sql.length() - 1) + whereStr;
    }

    public String getInsertSql() {
        String sql = "Insert into " + getRhid().getTabName();
        String strField = "(";
        String strValue = "Values(";

        List kList = getRhid().getViewClass().getDmKeyList();
        for (int i = 0; i < kList.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) kList.get(i);
            strField += dm.getColCode() + ",";
            if (dm.isInput()) {
                strValue += "'"
                        + StringUtil
                                .nullToStr(getDynaProperty(dm.getColCode()))
                        + "',";
            } else {
                strValue += "'" + com.ac.input.util.UUID.randomUUID()
                        + "',";
            }
        }

        List inList = getRhid().getCols();
        for (int i = 0; i < inList.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) inList.get(i);
            if (!dm.isKey()) {
                strField += dm.getColCode() + ",";
                String strTemp = StringUtil.nullToStr(getDynaProperty(dm
                        .getColCode()));
                if ("1".equals(dm.getVisTyp())) {
                    strValue += "'" + StringUtil.toSQL(strTemp) + "',";
                } else if ("3".equals(dm.getVisTyp())) {
                    strValue += "to_date('" + strTemp + "','yyyy-mm-dd'),";
                } else {
                    strValue += "'" + strTemp + "',";
                }
            }
        }
        strField = strField.substring(0, strField.length() - 1) + ")";
        strValue = strValue.substring(0, strValue.length() - 1) + ")";

        return sql + strField + strValue;
    }

    public String getOptDesc() {
        StringBuffer optDesc = new StringBuffer();

        List dmList = getRhid().getViewClass().getDmInList();
        for (int i = 0; i < dmList.size(); i++) {
            HndInpDefItm dm = (HndInpDefItm) dmList.get(i);
            String oldValue = StringUtil.nullToStr(dm.getOldValue());
            String newValue = StringUtil.nullToStr(getDynaProperty(dm
                    .getColCode()));
            if (!oldValue.equals(newValue)) {
                optDesc.append(dm.getColName()).append("=").append(oldValue)
                        .append(":").append(newValue).append(";");
            }
        }

        return optDesc.toString();
    }

    public String getKeyValue() {
        return keyValue;
    }

    public void setKeyValue(String keyValue) {
        this.keyValue = keyValue;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

	public String getAcPath() {
		return acPath;
	}

	public void setAcPath(String acPath) {
		this.acPath = acPath;
	}

	public String getIsDef() {
		return isDef;
	}

	public void setIsDef(String isDef) {
		this.isDef = isDef;
	}

	public Date getCurDate() {
		return curDate;
	}

	public void setCurDate(Date curDate) {
		this.curDate = curDate;
	}

	public String getIpAddr() {
		return ipAddr;
	}

	public void setIpAddr(String ipAddr) {
		this.ipAddr = ipAddr;
	}
}
